package com.weilele.leisure.video_factory.ui.module.common.dialog

import android.os.Bundle
import android.os.Parcelable
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.weilele.base.library.BaseBindingDialog
import com.weilele.leisure.time.R
import com.weilele.leisure.time.databinding.DialogPermissionTipsBinding
import com.weilele.leisure.video_factory.ui.module.common.holder.PermissionTipsItemHolder
import com.weilele.mvvm.adapter.refreshList
import com.weilele.mvvm.base.livedata.appCompatActivity
import com.weilele.mvvm.utils.activity.dp
import com.weilele.mvvm.utils.activity.goAppSettings
import com.weilele.mvvm.utils.fragment.arguments
import com.weilele.mvvm.utils.permission.callPermissions
import com.weilele.mvvm.utils.permission.checkPermission
import kotlinx.parcelize.Parcelize
import java.util.ArrayList

class PermissionTipsDialog : BaseBindingDialog<DialogPermissionTipsBinding>() {
    companion object {
        fun show(
            activity: AppCompatActivity,
            datas: MutableList<Bean>,
            onGetCall: Function0<Unit>
        ) {
            //检查权限
            if (activity.checkPermission(datas.flatMap { it.permissions })
                    .isEmpty()
            ) {
                onGetCall.invoke()
                return
            }
            PermissionTipsDialog().apply {
                permissionCall = onGetCall
                arguments {
                    putParcelableArrayList("datas", datas as ArrayList)
                }
            }.show(activity)
        }
    }

    @Parcelize
    data class Bean(
        var des: String,
        var permissions: MutableList<String>
    ) : Parcelable

    private var permissionCall: Function0<Unit>? = null
    private val noPermission by lazy {
        arguments?.getParcelableArrayList<Bean>("datas") ?: mutableListOf<Bean>()
    }

    private val rcvAdapter = PermissionTipsItemHolder.toAdapter()
    override fun onRootViewLayoutParams(lp: FrameLayout.LayoutParams) {
        super.onRootViewLayoutParams(lp)
        lp.marginStart = 24.dp
        lp.marginEnd = 24.dp
        lp.gravity = Gravity.CENTER
    }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.rcv.adapter = rcvAdapter
        rcvAdapter.refreshList(noPermission.map { it.des }.toMutableList())
    }

    override fun isCanceledOnTouchOutside(): Boolean {
        return false
    }

    override fun getClickView(): List<View?>? {
        return listOf(
            mBinding.btCancel,
            mBinding.btOk,
        )
    }

    override fun onSingleClick(view: View) {
        when (view) {
            mBinding.btCancel -> {
                dismiss()
            }
            mBinding.btOk -> {
                if (mBinding.btOk.text == getString(R.string.authorize)) {
                    callPermissions(noPermission.flatMap { it.permissions }) {
                        if (it.isNotEmpty()) {
                            //有权限拒绝
                            rcvAdapter.refreshList(noPermission.filter { bean ->
                                var hasPermission = true
                                it.forEach forPermission@{ p ->
                                    bean.permissions.forEach { s ->
                                        if (p == s) {
                                            hasPermission = false
                                            return@forPermission
                                        }
                                    }
                                }
                                !hasPermission
                            }.map { it.des }.toMutableList())
                            mBinding.btOk.setText(R.string.setting)
                            mBinding.tvDes.setText(R.string.no_permission_tips)
                        } else {
                            //已经获取全部权限
                            permissionCall?.invoke()
                            dismiss()
                        }
                    }
                } else {
                    appCompatActivity?.goAppSettings()
                    dismiss()
                }
            }
        }
    }
}